package com.micro.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableField;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@TableName("火车餐订单")
public class TrainMealOrder {
    @TableId(value = "订单ID", type = IdType.AUTO)
    private Long orderId;

    @TableField(value = "用户ID")
    private Long userId;

    @TableField(value = "列车ID")
    private Long trainId;

    @TableField(value = "座位号")
    private String seatNumber;

    @TableField(value = "订单编号")
    private String orderNumber;

    @TableField(value = "订单总金额")
    private BigDecimal totalAmount;

    @TableField(value = "订单状态")
    private String status; // 未支付、已支付、已取消、已完成

    @TableField(value = "创建时间")
    private LocalDateTime createTime = LocalDateTime.now();

    @TableField(value = "支付时间")
    private LocalDateTime paymentTime;

    @TableField(value = "备注")
    private String remark;

    @TableLogic(value = "deleted")
    private Integer deleted;

    // 订单明细列表 - 不映射到数据库，仅用于业务逻辑
    @TableField(exist = false)
    private List<TrainMealOrderItem> orderItems = new ArrayList<>();

    // 构造函数
    public TrainMealOrder() {
        this.status = OrderStatus.UNPAID.getValue();
    }

    // Getter和Setter方法
    public Long getOrderId() {
        return orderId;
    }

    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Long getTrainId() {
        return trainId;
    }

    public void setTrainId(Long trainId) {
        this.trainId = trainId;
    }

    public String getSeatNumber() {
        return seatNumber;
    }

    public void setSeatNumber(String seatNumber) {
        this.seatNumber = seatNumber;
    }

    public String getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(String orderNumber) {
        this.orderNumber = orderNumber;
    }

    public BigDecimal getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getPaymentTime() {
        return paymentTime;
    }

    public void setPaymentTime(LocalDateTime paymentTime) {
        this.paymentTime = paymentTime;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public List<TrainMealOrderItem> getOrderItems() {
        return orderItems;
    }

    public void setOrderItems(List<TrainMealOrderItem> orderItems) {
        this.orderItems = orderItems;
    }

    // 添加订单明细
    public void addOrderItem(TrainMealOrderItem item) {
        orderItems.add(item);
        item.setOrderId(this.orderId);
    }

    // 移除订单明细
    public void removeOrderItem(TrainMealOrderItem item) {
        orderItems.remove(item);
        item.setOrderId(null);
    }

    // 计算订单总金额
    public void calculateTotalAmount() {
        this.totalAmount = orderItems.stream()
                .map(item -> item.getPrice().multiply(new BigDecimal(item.getQuantity())))
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    // 订单状态枚举
    public enum OrderStatus {
        UNPAID("未支付"),
        PAID("已支付"),
        CANCELLED("已取消"),
        COMPLETED("已完成");

        private final String value;

        OrderStatus(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }

    public Integer getDeleted() {
        return deleted;
    }

    public void setDeleted(Integer deleted) {
        this.deleted = deleted;
    }
}
